﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CPPEI.WebApi
{
    public abstract class BaseAction : IAction
    {
        public string HttpMethod
        {
            get;
            internal set;
        }

        public bool IsAnonymous
        {
            get;
            internal set;
        }
        public ActionResult Execute(HttpRequest request, SysParameters sysParams)
        {
            ActionContext context = new ActionContext();
            context.IP = request.UserHostAddress;
            context.UserId = Intergration.Config.AuthProvider.GetUserId();
            context.HttpRequest = request;

            return OnExecute(request, context, sysParams);
        }

        protected abstract ActionResult OnExecute(HttpRequest request, ActionContext context, SysParameters sysParams);
    }

    public abstract class BaseAction<TParam, TResult> : BaseAction
    {
        protected override ActionResult OnExecute(HttpRequest request, ActionContext context, SysParameters sysParams)
        {
            // check is auth
            if (!this.IsAnonymous && !Intergration.Config.AuthProvider.IsAuth())
            {
                return new ActionResult(null, false, ErrorCode.Auth_NoAuth, ErrorCode.ToString(ErrorCode.Auth_NoAuth), null);
            }

            if (!string.Equals(request.HttpMethod, this.HttpMethod, StringComparison.CurrentCultureIgnoreCase))
            {
                return new ActionResult("1.0", false, ErrorCode.InvalidHttpMethod, "HttpMethod不匹配!", null);
            }
            else if (string.Equals(HttpMethods.POST, this.HttpMethod))
            {
                TParam opParams = (TParam)(typeof(TParam).Assembly.CreateInstance(typeof(TParam).FullName));

                try
                {
                    ParameterInjection.Injection4Post(request, opParams);
                }
                catch (Exception ex)
                {
                    return new ActionResult(false, ErrorCode.Common_InvalidParameter, ErrorCode.ToString(ErrorCode.Common_InvalidParameter), null);
                }

                ActionResult<TResult> actionResult = OnExecute(context, sysParams, opParams);

                return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
            }
            else if (string.Equals(HttpMethods.GET, this.HttpMethod))
            {
                TParam opParams = (TParam)(typeof(TParam).Assembly.CreateInstance(typeof(TParam).FullName));
                try
                {
                    ParameterInjection.Injection4Get(request, opParams);
                }
                catch (Exception ex)
                {
                    return new ActionResult(false, ErrorCode.Common_InvalidParameter, ErrorCode.ToString(ErrorCode.Common_InvalidParameter), null);
                }

                ActionResult<TResult> actionResult = OnExecute(context, sysParams, opParams);

                return new ActionResult(actionResult.ActionVersion, actionResult.IsSccuess, actionResult.ErrorCode, actionResult.Message, actionResult.Data);
            }
            else
            {
                return new ActionResult("1.0", false, ErrorCode.InvalidHttpMethod, "HttpMethod不匹配!", null);

            }
        }

        protected abstract ActionResult<TResult> OnExecute(ActionContext context, SysParameters sysParams, TParam opParams);
    }
}
